#include "util.h"

extern map <string, int> s2n; // mapping from strings to numbers
extern vector <string> n2s; // reverse mapping

extern map <string,int> dict,gaz;

extern vector <bool> fdi, fga, fcap, fment, fing;
extern vector <string> fg;

double power(double a, int n) {
   double r=1;
   for (int i=0; i<n; ++i) r *= a;
   return r;
}

int mapInsert(const string &s) {
   map<string,int>::iterator itr=s2n.find(s);
   if (itr==s2n.end()) {
      s2n[s] = n2s.size();
      n2s.push_back(s);

      // include features
      
      if (s.size() > 0) {
	 fcap.push_back(s[0] >= 'A' && s[0] <= 'Z');
      }
      else fcap.push_back(false);
      
      string ds = decap(s);
      map<string,int>::iterator itr=dict.find(ds);
      fdi.push_back(itr != dict.end());

      itr = gaz.find(s);
      fga.push_back(itr != gaz.end());

      if (s.size() < 4) fment.push_back(false);
      else {
	 int i=s.size()-1;
	 if (s[i-3]=='m' && s[i-2]=='e' && s[i-1]=='n' && s[i]=='t') {
	    fment.push_back(true);
	 }
	 else fment.push_back(false);
      }
      if (s.size() < 3) fing.push_back(false);
      else {
	 int i=s.size()-1;
	 if (s[i-2]=='i' && s[i-1]=='n' && s[i]=='g') {
	    fing.push_back(true);
	 }
	 else fing.push_back(false);
      }

      fg.push_back(gFeature(s));

      return n2s.size()-1;
   }
   else return itr->second;
}

std::string decap(std::string s) {
   return s;
   for (int i=0; i<s.size(); ++i) {
      if (s[i]>=65 && s[i]<=90) s[i] += 32;
   }
   return s;
}

string gFeature(const string &s) {
   string r;
   bool a=false, A=false, zero=false;
   for (int i=0; i<s.size(); ++i) {
      if (s[i] >= 'a' && s[i] <= 'z') {
	 if (! a) {
	    r.push_back('a');
	    a=true;
	 }
      }
      else a=false;
      if (s[i] >= 'A' && s[i] <= 'Z') {
	 if (! A) {
	    r.push_back('A');
	    A=true;
	 }
      }
      else A=false;
      if (s[i] >= '0' && s[i] <= '9') {
	 if (! zero) {
	    r.push_back('0');
	    zero=true;
	 }
      }
      else zero=false;

      if (!a && !A && !zero) {
	 r += "_ASCII";
	 r += itos((int) s[i]);
	 r += "_";
      }
   }
   return r;
}

